<%@ page  pageEncoding="utf-8"%>
<%@page import="org.myhibernate.Template"%>
<%@page import="org.myhibernate.ProxyTemplate"%>
<%@page import="org.myhibernate.core.method.PageSizeQuery"%>
<%@page import="java.util.List"%>
<%@page import="org.json.JSONArray"%>
<%@page import="org.json.JSONObject"%>
<%@page import="org.personal.util.UserUtil"%>
<%@page import="org.personal.Constants"%>
<%@page import="org.personal.entity.Layout"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
int start=0,size=15,count=0;
if(request.getParameter("start")!=null){start=Integer.parseInt(request.getParameter("start"));}
if(request.getParameter("size")!=null){size=Integer.parseInt(request.getParameter("size"));}
UserUtil userUtil=(UserUtil)request.getSession().getAttribute(Constants.USER_UTIL);
	if(!userUtil.hasSystemAdmin(request))
	{
		response.sendRedirect("../error.jsp");
	}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>布局管理</title> 
<%@include file="../styles/styles.jsp" %>
<script src="<%=basePath %>js/select.js" ></script>
  </head>
  <body>
     <table cellspacing="0" cellpadding="0" class="tab" id="userform" style="width:100%;display:;"  >
	<tr>
	<td>
	<input id="key" style="width:200px;"  value="" />
	<input type="button"   class="mybutton" value=" 搜 索 " onclick="pageNavCall(1)" style="" />
	&nbsp;&nbsp;
	<input type="button"   class="mybutton" value=" 刷 新 " onclick="pageNavCall(1)" style="" />
	&nbsp;&nbsp;
	<input type="button"   class="mybutton" value=" 新 建 " onclick="buttonAction(0)" style="" />
	&nbsp;&nbsp;
				<span id="message" ></span>
	</td></tr>
	</table>
	<br/>
     <div id="listtable" >
  <table cellspacing="0" cellpadding="0" class="tab"  style="width:100%;display:;"  >
  <thead>
	<tr><td>标识</td><td>名称</td><td>页面路径</td><td>是否默认</td><td>操作</td></tr></thead>
<%
   	Template template=new ProxyTemplate(Layout.class).getInstance();
	PageSizeQuery query=template.getPageSizeQuery();
	List list=null;
	Layout layout=null;
	count=query.query("");
	list=query.getListObject(start,start+size);

   	try
   	{
   		for(int i=0;i<list.size();i++)
   		{
   			layout=(Layout)list.get(i);
   			%>
				<tr>
					<td><%=layout.getId()%></td>
    				<td><%=layout.getTitle()%></td>
    				<td><%=layout.getPagePath()%></td>
    				<td><%=layout.getDefaultLayout()%></td>
					<td>
    				<a href="javascript:;" onclick="actionFunction('edit','<%=layout.getId()%>')" >编辑</a>&nbsp;&nbsp;
    				<a href="javascript:;" onclick="actionFunction('delete','<%=layout.getId()%>','<%=layout.getTitle()%>')">删除</a>
    				&nbsp;&nbsp;
    				<a href="javascript:;" onclick="actionFunction('reflesh','<%=layout.getId()%>','<%=layout.getTitle()%>')">刷新</a>
    				</td>
    				</tr>   			
   			<%
   		}
   	}catch(Exception exception){}
   	finally
   	{
   		template.close();
   	}	
   	if(count == 0)
    {
    	out.println("<tr><td colspan=4 align=center >暂无数据</td></tr>");
    }	
%>
</table>
<div id="pagenav"></div>
   </div>
   <br/>
   
  <table cellspacing="0" cellpadding="0" class="tab" id="datatable" style="width:100%;display:none;"  >
				<tbody>
				<tr>
				<td>名称<font color=red>*</font></td>
				<td align="left" >
					<input id="title" style="width:400px;" />
				</td>
				</tr>
				<tr>
				<td>页面路径<font color=red>*</font></td>
				<td align="left" >
				<input id="pagePath" style="width:400px;" />
				</td>
				</tr>
				<tr>
				<td>设置默认</td>
				<td align="left" >
					<select id="defaultLayout"  style="width:100px;" >
						<option value="false">否</option>
						<option value="true">是</option>
					</select>
				</tr>
				<tr>
				<td>&nbsp;</td><td align="left" >
				<input type="button"   class="mybutton" value="保存" onclick="saveFunc()" style="" />
				&nbsp;&nbsp;
				<input type="button"   class="mybutton" value="清空" onclick="resetFunc(0)" style="" />
				&nbsp;&nbsp;
				<input type="button"   class="mybutton" value="隐藏" onclick="resetFunc(1)" style="" />
				</td>
				</tr>
				</table>   
<script>
var map=new Object();
var start=<%=start%>;
var size=<%=size%>;
var count=<%=count%>;
var currentPage=(start/size)+1;
function get(id){return document.getElementById(id);}
var message=get("message");
function pageNavCall(index)
{
	//alert(index);
	var tempStart=(index-1)*size;
	var args="start="+tempStart+"&size="+size;
	args=encodeURI(encodeURI(args));
	window.location.href="<%=basePath%>admin/layouts.jsp?"+args;
}
var pageNav=new PageNavgation();
pageNav.setHandle(pageNav);
pageNav.setCall(pageNavCall);
pageNav.setAllCount(count);
pageNav.setPageSize(size);
pageNav.setCurrentPage(currentPage);
pageNav.bind("pagenav");
var currentId="";
function saveFunc()
{
	var title=get("title").value;
	var pagePath=get("pagePath").value;
	var defaultLayout=get("defaultLayout").value;
	
	if(title.Trim().length == 0){message.innerHTML="名称不能为空";return;}
	if(pagePath.Trim().length == 0){message.innerHTML="页面路径不能为空";return;}
	
	var args="";
	args=args+"id="+currentId;
	args=args+"&title="+title;
	args=args+"&pagePath="+pagePath;
	args=args+"&defaultLayout="+defaultLayout;
	args=encodeURI(encodeURI(args));
	message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
	new doCallRequest("<%=basePath%>action/saveOrUpdateLayout.jsp",args,function(data)
	{
		var json=toJson(data);
		if("ok" == json.result)
		{
			window.location.reload();
		}else
		{
			message.innerHTML=json.message;
		}
	});
	
}

function setFormData(data)
{
	message.innerHTML="";
	var item=toJson(data);
	if( "ok" == item.result )
	{
		map[item.data.id]=data;
		currentId=item.data.id;
		get("title").value=item.data.title;
		get("pagePath").value=item.data.pagePath;
		setSelectSelected("defaultLayout",item.data.defaultLayout+"");
		
	}else
	{
		message.innerHTML=item.message;
	}
}
function actionFunction(type,id,title)
{
	if("edit" == type)
	{
		get("datatable").style.display="";
		var item=map[id];
		if(item != undefined )
		{
			setFormData(item);
		}else
		{
			message.innerHTML='<img src="<%=basePath%>images/loading.gif" />';
			new doCallRequest("<%=basePath%>action/getLayout.jsp","id="+id,setFormData);
		}
	}else if("delete" == type)
	{
		if(confirm("确定删除系统？"+title))
		{
			message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
			var args="classname=com.portal.entity.Layout&id="+id;
			new doCallRequest("<%=basePath%>org/myhibernate/action/delete.jsp",args,function(data)
			{
				var json=toJson(data);
				if("ok" == json.result)
				{
					window.location.reload();
				}else
				{
					message.innerHTML=json.message;
				}
				
			});
		}
	}else if("reflesh" == type)
	{
			message.innerHTML='<img src="<%=basePath%>images/loading.gif" border=0 />';
			var args="id="+id;
			new doCallRequest("<%=basePath%>action/reloadLayout.jsp",args,function(data)
			{
				var json=toJson(data);
				if("ok" == json.result)
				{
					message.innerHTML='刷新成功';
				}else
				{
					message.innerHTML=json.message;
				}
				
			});
	}
}

function resetFunc(index)
{
	if(0 == index)
	{
		get("title").value="";
		get("pagePath").value="";
	}else if(1 == index)
	{
		get("datatable").style.display="none";
	}
}
function buttonAction()
{
	get("datatable").style.display="";
	currentId="";
	resetFunc(0);
}
</script>
  </body>
</html>
